package com.syh.core.ddd.domain;

import cn.redscarf.commons.util.other.IdWorker;

import java.io.Serializable;

/**
 * 抽象领域实体，重写equals 与 hashCode
 *
 * @param <ID>
 */
public abstract class AbstractEntity<ID extends Serializable> implements EntityObject<ID> {

  protected ID id;
  protected String uuid;


  /**
   * 静态方法 与类的对象行为无关,主要是用户统一生成的路径id
   *
   * @return
   */
  public static Long nextLongId() {
    return IdWorker.quickNextId();
  }

  @Override
  public boolean equals(Object obj) {

    if (null == obj) {
      return false;
    }

    if (this == obj) {
      return true;
    }

    if (!getClass().equals(obj.getClass())) {
      return false;
    }

    AbstractEntity<?> that = (AbstractEntity<?>) obj;

    return null == this.getId() ? false : this.getId().equals(that.getId());
  }

  @Override
  public int hashCode() {

    int hashCode = 17;

    hashCode += null == getId() ? 0 : getId().hashCode() * 31;

    return hashCode;
  }

  @Override
  public ID getId() {
    return id;
  }

  @Override
  public AbstractEntity setId(ID id) {
    this.id = id;
    return this;
  }


  @Override
  public String getUuid() {
    return uuid;
  }

  public AbstractEntity<ID> setUuid(String uuid) {
    this.uuid = uuid;
    return this;
  }


}
